﻿#include "GUISpectraEnergyDialog.h"
#include "ui_GUISpectraDialogBase.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKSpectraEnergy.h"

namespace GUI
{
    GUISpectraEnergyDialog::GUISpectraEnergyDialog(int spectraDataID, bool isCreate) :
        GUISpectraDialogBase(spectraDataID, isCreate)
    {
        //初始化操作
        init();
    }

    GUISpectraEnergyDialog::~GUISpectraEnergyDialog()
    {

    }

    void GUISpectraEnergyDialog::init()
    {
        Acoustics::FITKSpectraEnergy* spectra = new Acoustics::FITKSpectraEnergy();
        //设置图标
        _ui->label_image->setPixmap(QPixmap(":/browserIcons/energy.png"));
        //初始化界面默认数据
        _ui->radioButton_RMS->setText(tr("Energy in N-m"));
        _ui->radioButton_PSD1->setText(tr("PSD in N-m/Hz"));
        _ui->radioButton_PSD2->setText(tr("PSD in g^2/Hz"));
        _ui->radioButton_PSD2->setEnabled(false);
        _uiCurrentZeroDBValue = spectra->getDBDefaultCoefficient();
        _ui->lineEdit_Default->setText(QString::number(_uiCurrentZeroDBValue));
        _ui->lineEdit_UserDefined->setText(QString::number(_uiCurrentZeroDBValue));
        _ui->radioButton_RMS->setChecked(true);
        _ui->radioButton_FrequencyLog->setChecked(true);
        // 设置曲线画布标题
        setPlotTitle(tr("Energy"));
        //设置单位
        _RSMUnit = tr("1/m");

        //创建界面
        if (_isCreate) {
            spectra->init();
            _spectraUiDataID = spectra->getDataObjectID();
            QString name = "";
            if (_spectraManager) name = _spectraManager->checkName("New Energy Spectrum");
            _ui->lineEdit_Name->setText(name);
            //初始化数据
            updateHzValue(spectra->getSpectraValue());
            //更新form类型
            upateFormType();
            //更新曲线Y轴标签
            updatePlotYTitle();
            // 设置窗口标题
            setWindowTitle(tr("Create Energy Spectrum"));
        }
        else {
            //初始化数据
            Acoustics::FITKAcousticsAbstractSpectra* currentSpectra = getCurrentSpectraData();
            if (currentSpectra) {
                _spectraUiDataID = spectra->getDataObjectID();
                setDataToWidget(currentSpectra);
                //初始化界面默认数据
                _ui->lineEdit_Name->setText(spectra->getDataObjectName());
            }
            // 设置窗口标题
            setWindowTitle(tr("Edit Energy Spectrum"));
        }
        //更新单位
        updateZeroDBUnit();
        //初始化信号槽连接
        initConnect();
    }

    void GUISpectraEnergyDialog::upateFormType()
    {
        QString domainType = tr("");
        if (_ui->radioButton_RMS->isChecked()) {
            domainType = tr("Band-limited power spectrum");
        }
        else if (_ui->radioButton_PSD1->isChecked()) {
            domainType = tr("Band-limited power spectrum");
        }
        _ui->lineEdit_Form->setText(domainType);
    }

    void GUISpectraEnergyDialog::updatePlotYTitle()
    {
        QString psdTitle = "";
        if (_ui->radioButton_RMS->isChecked()) {
            psdTitle = tr("N-m");
        }
        else if (_ui->radioButton_PSD1->isChecked()) {
            psdTitle = tr("N-m / Hz");
        }

        QString title = "";
        if (_ui->checkBox_dB->isChecked()) {
            if (_ui->checkBox_AWeighting->isChecked()) {
                title = tr("dBA(ref = %1 %2)").arg(_uiCurrentZeroDBValue).arg(psdTitle);
            }
            else {
                title = tr("dB(ref = %1 %2)").arg(_uiCurrentZeroDBValue).arg(psdTitle);
            }
        }
        else
        {
            title = psdTitle;
        }
        this->setPlotYTitle(title);
    }

    void GUISpectraEnergyDialog::updateZeroDBUnit()
    {
        QString unit = "";
        if (_ui->radioButton_RMS->isChecked()) {
            unit = tr("N-m");
        }
        else if (_ui->radioButton_PSD1->isChecked()) {
            unit = tr("N-m/Hz");
        }

        _ui->label_DefaultUnit->setText(unit);
        _ui->label_UserUnit->setText(unit);
        updateConstantAndRealValueUnit(unit);
    }

    Acoustics::FITKAcousticsAbstractSpectra * GUISpectraEnergyDialog::createNewSpectra()
    {
        return new Acoustics::FITKSpectraEnergy();
    }

    void GUISpectraEnergyDialog::setDataToWidget(Acoustics::FITKAcousticsAbstractSpectra * data)
    {
        GUISpectraDialogBase::setDataToWidget(data);
        auto spectraData = dynamic_cast<Acoustics::FITKSpectraEnergy*>(data);
        if (spectraData == nullptr)return;

        //PSD
        if (spectraData->getFormCalcType() == 0) {
            _ui->radioButton_RMS->setChecked(true);
        }
        else if (spectraData->getFormCalcType() == 1) {
            _ui->radioButton_PSD1->setChecked(true);
        }
        else if (spectraData->getFormCalcType() == 2)
        {
            _ui->radioButton_PSD2->setChecked(true);
        }

        //DB
        if (spectraData->getDBEnableState() == true) {
            _ui->widget_LogAndLin->setEnabled(false);
            _ui->radioButton_Linear->setChecked(true);
            _ui->checkBox_AWeighting->setEnabled(true);
            _ui->groupBox_ZeroDB->setEnabled(true);
            _ui->checkBox_dB->setChecked(true);
            if (spectraData->getAWeightingEnableState() == true) {
                _ui->checkBox_AWeighting->setChecked(true);
            }
            else {
                _ui->checkBox_AWeighting->setChecked(false);
            }
        }
        else {
            _ui->widget_LogAndLin->setEnabled(true);
            _ui->checkBox_AWeighting->setEnabled(false);
            _ui->groupBox_ZeroDB->setEnabled(false);
            _ui->checkBox_dB->setChecked(false);
            _ui->checkBox_AWeighting->setChecked(false);
        }

        //零DB
        updateZeroDBWidget(spectraData->getDBUserCoefficient());
        _ui->lineEdit_Default->setText(QString::number(spectraData->getDBDefaultCoefficient()));
        if (spectraData->isDBUserDefined() == true) {
            _uiCurrentZeroDBValue = spectraData->getDBUserCoefficient();
        }
        else {
            _uiCurrentZeroDBValue = spectraData->getDBDefaultCoefficient();
        }
        _ui->lineEdit_UserDefined->setText(QString::number(_uiCurrentZeroDBValue));

        //更新曲线Y轴标题
        updatePlotYTitle();
        //更新form类型
        upateFormType();
        //计算并刷新界面频率值
        frontCalculate();
    }

    void GUISpectraEnergyDialog::getDataFromWidget(Acoustics::FITKAcousticsAbstractSpectra* data)
    {
        GUISpectraDialogBase::getDataFromWidget(data);
        auto spectraData = dynamic_cast<Acoustics::FITKSpectraEnergy*>(data);
        if (spectraData == nullptr)return;

        if (_ui->radioButton_RMS->isChecked()) {
            spectraData->setFormCalcType(0);
        }
        else if (_ui->radioButton_PSD1->isChecked()) {
            spectraData->setFormCalcType(1);
        }
        else if (_ui->radioButton_PSD2->isChecked())
        {
            spectraData->setFormCalcType(2);
        }

        //存储DB
        if (_ui->checkBox_dB->isChecked()) {
            spectraData->setDBEnableState(true);
            if (_ui->checkBox_AWeighting->isChecked()) {
                spectraData->setAWeightingEnableState(true);
            }
        }
        else {
            spectraData->setDBEnableState(false);
            spectraData->setAWeightingEnableState(false);
        }

        //存储零DB
        const double EPS = 0.0000001;
        if ((_uiCurrentZeroDBValue - 0.0 <= EPS) || (!(_uiCurrentZeroDBValue > 1.0 || _uiCurrentZeroDBValue < 1.0))) {
            spectraData->setDBUserCoefficient(-1);
        }
        else {
            spectraData->setDBUserCoefficient(_uiCurrentZeroDBValue);
        }
    }
}
